<template>
  <div class="GoodsSty">
    <el-breadcrumb
      style="font-size: 16px; padding: 20px 40px"
      separator-class="el-icon-arrow-right"
    >
      <el-breadcrumb-item to="/">首页</el-breadcrumb-item>
      <el-breadcrumb-item>全部商品</el-breadcrumb-item>
      <el-breadcrumb-item v-show="!search">分类</el-breadcrumb-item>
      <el-breadcrumb-item v-show="search">搜索</el-breadcrumb-item>
      <el-breadcrumb-item v-show="search">{{ search }}</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 分类 -->
    <div class="claify">
      <p>
        <el-tabs v-model="activeName" type="card">
          <el-tab-pane
            :label="i.category_name"
            :name="'' + i.category_id"
            v-for="i in categoryList"
            :key="i.category_id"
          >
            <Good :ProductData="product" :isMore="true" @fadect="fadect"></Good>
            <p v-show="flag">抱歉没有找到相关的商品，请看看其他的商品</p>
            <el-pagination
              background
              layout="prev, pager, next"
              :page-size="pageSize"
              :total="total"
              @current-change="currentChange"
            />
          </el-tab-pane>
        </el-tabs>
      </p>
    </div>
  </div>
</template>

<script>
import Good from "@/components/General/SingleGood.vue";
import goodsApi from "@/api/PageAPI/GoodsAPI.js";
export default {
  components: { Good },
  data() {
    return {
      search: "",
      product: [],
      activeName: "0",
      categoryList: [],
      categoryID: [],
      currentPage: 1,
      pageSize: 15,
      total: 0,
      flag: false,
    };
  },
  created() {
    this.getCategory();
    //获取全部商品
    this.getProduct("getAllProduct");
  },
  mounted() {
    // 浏览更多
    let categId = this.$variate.categId;
    // console.log(categId);
    if (categId) {
      this.categoryID = categId;
      this.getProduct();
    }
  },
  activated() {
    this.activeName = "-1";
  },

  methods: {
    fadect(id) {
      console.log(id);
      // 获取到当前商品的id后路由传参
      this.$router.push(`/details/${id}`);
    },
    //获取商品分类信息
    async getCategory() {
      let res = await goodsApi.getCategory();
      this.categoryList = res.category;
      this.categoryList.unshift({
        category_id: 0,
        category_name: "全部",
      });
      // console.log(this.categoryList);
    },
    //获取所有商品
    async getProduct(apiName = "getProductByCategory") {
      let sendData = {
        categoryID: this.categoryID,
        currentPage: this.currentPage,
        pageSize: this.pageSize,
      };

      let res = await goodsApi[apiName](sendData);
      this.product = res.Product;
      this.total = res.total;
    },

    // 页码变化调用currentChange方法
    currentChange(currentPage) {
      this.currentPage = currentPage;
    },
    //根据搜索条件获取数据
    async getProductBySearch() {
      let sendData = {
        search: this.search,
        currentPage: this.currentPage,
        pageSize: this.pageSize,
      };
      let res = await goodsApi.getProductBySearch(sendData);
      this.product = res.Product;
      this.total = res.total;
      // 搜索条件没有时 显示友好提示
      if (res.total == 0) {
        this.flag = true;
      }
    },
  },
  watch: {
    $route(val) {
      console.log(val);
      // if (this.$store.state.query.categoryID) {
      // this.categoryID = this.$store.state.query.categoryID;
      // console.log(this.categoryID);
      // this.getProduct();
      // return;
      // }
      // 搜索
      this.search = val.query.search;
      this.getProductBySearch();
    },

    activeName(value) {
      //切换分类
      let categoryID = Number(value);
      this.categoryID = [categoryID];

      //每次切换分类都要重置分页数据
      this.currentPage = 1;
      this.total = 0;
      this.search = "";

      //根据分类ID确定是获取所有商品还是单独某一类的商品
      if (categoryID === 0) {
        //获取全部商品
        this.categoryID = [];
        this.getProduct("getAllProduct");
      } else {
        this.getProduct();
      }
    },
    //分页被改变的时候
    currentPage() {
      if (!this.categoryID.length) {
        this.getProduct("getAllProduct");
        return;
      }
      this.getProduct();
    },

    //监听路由
  },
};
</script>

<style lang="scss" scoped>
.GoodsSty {
  .claify {
    text-align: center;
    strong {
      height: 40px;
      line-height: 40px;
      margin-right: 20px;
    }
    .el-tab-pane {
      // height: 500px;
      background: #ddd;
      padding: 20px 50px;
      margin-bottom: 20px;
    }
    .el-pagination {
      margin-top: 20px;
    }
  }
}
</style>
